home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / ppm / ppmhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  1.9 KB  |  78 lines

  1. /* ppmhist.c - read a portable pixmap and compute a color histogram
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #include "ppmcmap.h"
  15.  
  16. #define MAXCOLORS 100000
  17.  
  18. static int
  19. countcompare( ch1, ch2 )
  20. colorhist_vector ch1, ch2;
  21.     {
  22.     return ch2->value - ch1->value;
  23.     }
  24.  
  25. void
  26. main( argc, argv )
  27.     int argc;
  28.     char* argv[];
  29.     {
  30.     FILE* ifp;
  31.     pixel** pixels;
  32.     colorhist_vector chv;
  33.     int argn, rows, cols, colors, i;
  34.     pixval maxval;
  35.     int countcompare();
  36.     char* usage = "[ppmfile]";
  37.  
  38.     ppm_init( &argc, argv );
  39.  
  40.     argn = 1;
  41.  
  42.     if ( argn != argc )
  43.     {
  44.     ifp = pm_openr( argv[argn] );
  45.     argn++;
  46.     }
  47.     else
  48.     ifp = stdin;
  49.  
  50.     if ( argn != argc )
  51.     pm_usage( usage );
  52.  
  53.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  54.  
  55.     pm_close( ifp );
  56.  
  57.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  58.     if ( chv == (colorhist_vector) 0 )
  59.     pm_error( "too many colors - try doing a ppmquant" );
  60.  
  61.     /* Sort by count. */
  62.     qsort( (char*) chv, colors, sizeof(struct colorhist_item), countcompare );
  63.  
  64.     /* And print the histogram. */
  65.     printf( " r   g   b \tlum\tcount\n" );
  66.     printf( "--- --- ---\t---\t-----\n" );
  67.     for ( i = 0; i < colors; i++ )
  68.     printf(
  69.         "%3d %3d %3d\t%d\t%d\n", PPM_GETR(chv[i].color),
  70.         PPM_GETG(chv[i].color), PPM_GETB(chv[i].color),
  71.         (int) ( PPM_LUMIN( chv[i].color ) + 0.5 ),
  72.         chv[i].value );
  73.     
  74.     ppm_freecolorhist( chv );
  75.  
  76.     exit( 0 );
  77.     }
  78.